package com.emagic.manage.data.datasource;

import android.content.Context;
import android.text.TextUtils;

import com.emagic.manage.data.cache.ICache;
import com.emagic.manage.data.entities.response.Empty;
import com.emagic.manage.data.entities.response.OutOffResponse;
import com.emagic.manage.data.entities.response.RoomsResponse;
import com.emagic.manage.data.entities.response.UserResponse;
import com.emagic.manage.data.network.Config;
import com.emagic.manage.data.network.NetworkApi;
import com.emagic.manage.data.network.SSLContextUtil;
import com.emagic.manage.data.network.adapter.RxJavaCallAdapterFactory;
import com.google.gson.Gson;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.inject.Inject;
import javax.inject.Singleton;

import okhttp3.OkHttpClient;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;
import rx.Observable;

/**
 * 实现数据层接口,restful api接口数据提供
 *
 * @author baoy
 * @version 1.0
 *          Create by 15/12/11 下午3:10
 */
@Singleton
public class RestDataSource implements DataSource {

    private final NetworkApi networkApi;

    private Map<String, ICache> mCacheMap;
    private Gson mGson;

    private Context mContext;

    @Inject
    public RestDataSource(Context context, OkHttpClient client) {

        this.mContext = context;
        mCacheMap = new HashMap<>();
        mGson = new Gson();
        OkHttpClient.Builder builder = client.newBuilder()
                .sslSocketFactory(SSLContextUtil.getDefaultSLLContext().getSocketFactory());
        client = builder.build();

        Retrofit retrofit = new Retrofit.Builder().baseUrl(Config.getAppApiUrl())
                .addConverterFactory(GsonConverterFactory.create())
                .addCallAdapterFactory(RxJavaCallAdapterFactory.create())
                .client(client)
                .build();

        networkApi = retrofit.create(NetworkApi.class);
    }

    public void putCache(String key, ICache cache) {
        mCacheMap.put(key, cache);
    }

    private String nullFormat(String source) {
        return TextUtils.isEmpty(source) ? "" : source;
    }

    private Object nullFormat(List<?> list) {
        if (list == null) {
            return new ArrayList<>(0);
        } else return list;
    }

    private Object nullFormat(Map map) {
        if (map == null) {
            return new HashMap<>();
        } else return map;
    }

    @Override
    public Observable<UserResponse> loginapi(String phone, String pwd, String cidentifier, String alias) {
        return networkApi.loginapi(nullFormat(phone), nullFormat(pwd));
    }

    @Override
    public Observable<RoomsResponse> getallroom(String userid) {
        return networkApi.getallroom(nullFormat(userid));

    }

    @Override
    public Observable<OutOffResponse> logOut(String userid, String sessionid) {
        return networkApi.logOut(nullFormat(userid), nullFormat(sessionid));
    }

    @Override
    public Observable<OutOffResponse> getroom(String serial) {
        return networkApi.getroom(nullFormat(serial));
    }

    @Override
    public Observable<Empty> sendgift(String serial, String sendid, String sendname, Map studios) {
        return networkApi.sendgift(nullFormat(serial),nullFormat(sendid) ,nullFormat(sendname) , nullFormat(studios));
    }
}
